'use strict';

const Controller = require('egg').Controller;
class UserController extends Controller {
  async selectAll() { // 查询所有的用户
    const { ctx } = this;
    const list = await ctx.service.userService.selectAll();
    ctx.body = { list };
  }
  // 用户注册的方法
  async register() {
    const { ctx } = this;
    let data = '';
    const userName = ctx.request.body.userName;
    const userPwd = ctx.request.body.userPwd;
    const nickname = ctx.request.body.nickname;
    const userPortrait = ctx.request.body.userPortrait;
    const realName = ctx.request.body.realName;
    const userSex = ctx.request.body.userSex;
    const borrowNumber = ctx.request.body.borrowNumber;
    const borrowHistory = ctx.request.body.borrowHistory;// 借阅历史
    // const adminPortrait = ctx.request.adminPortrait;
    console.log(ctx.request.body.userName);
    if (!userName) {
      ctx.body = { code: 501, msg: '缺少必填项;账号不能为空' };
    } else if (!userPwd) {
      ctx.body = { code: 501, msg: '缺少必填项;密码不能为空' };
    } else {
      data = await ctx.service.userService.register({
        userName,
        userPwd,
        nickname,
        userPortrait,
        realName,
        borrowNumber,
        userSex,
        borrowHistory,
      });
      ctx.body = { code: 200, data };
    }
  }
  async login() { // 用户登录
    const { ctx } = this;
    const userName = ctx.request.body.userName;
    const userPwd = ctx.request.body.userPwd;
    if (!userName) {
      ctx.body = { code: 501, msg: '用户名不能为空' };
    } else if (!userPwd) {
      ctx.body = { code: 501, msg: '密码不能为空' };
    } else {
      const data = await ctx.service.userService.login({
        userName,
        userPwd,
      });
      ctx.body = { code: 200, data };
    }
  }
  // 用户修改全部的个人信息(管理员负责调用,不可以修改用户userName和用户密码)
  async updateByAdmin() {
    const { ctx } = this;
    let data = '';
    const userName = ctx.request.body.userName;
    const userPortrait = ctx.request.body.userPortrait;
    const userSex = ctx.request.body.userSex;
    const borrowNumber = ctx.request.body.borrowNumber;
    const nickname = ctx.request.body.nickname;
    const borrowHistory = ctx.request.body.borrowHistory;
    const realName = ctx.request.body.realName;
    // const adminPortrait = ctx.request.adminPortrait;
    if (!userName) {
      ctx.body = { code: 501, msg: '缺少必填字段;请输入用户名字段!' };
    } else {
      data = await ctx.service.userService.updateByAdmin({
        userName,
        userPortrait,
        userSex,
        borrowNumber,
        nickname,
        borrowHistory,
        realName,
      });
      ctx.body = { code: 200, data };
    }
  }
  // 用户修改全部的个人信息(用户负责调用,不可以修改用户userName和借阅信息和借书数量)
  async updateByUser() {
    const { ctx } = this;
    let data = '';
    const userName = ctx.request.body.userName;
    const userPwd = ctx.request.body.userPwd;
    const userPortrait = ctx.request.body.userPortrait;
    const userSex = ctx.request.body.userSex;
    const realName = ctx.request.body.realName;
    const nickname = ctx.request.body.nickname;
    if (!userName) {
      ctx.body = { code: 501, msg: '缺少必填字段;请输入用户名字段!' };
    } else {
      data = await ctx.service.userService.updateByUser({
        userName,
        userPwd,
        userPortrait,
        userSex,
        nickname,
        realName,
      });
      ctx.body = { code: 200, data };
    }
  }
  // 用户删除的方法(管理员负责调用,根据用户userName查询)
  async delUserByUsername() {
    const { ctx } = this;
    const { username } = ctx.params;// 获取delete的数据
    console.log(ctx.params);
    console.log(username);
    if (!username) {
      ctx.body = { code: 501, msg: '缺少必填字段;请输入用户名字段!' };
    } else {
      const data = await ctx.service.userService.deleteUser(username);
      ctx.body = { code: 200, data };
    }
  }
  // 根据用户名称查找用户信息
  async findByUserName() {
    const { ctx } = this;
    const userName = ctx.request.body.userName;
    if (!userName) {
      ctx.body = { code: 501, msg: '缺少必填字段;请输入userName字段!' };
    } else {
      const data = await ctx.service.userService.findByUserName(userName);
      ctx.body = { code: 200, data };
    }
  }
  // 分页查询用户
  async limitUser() {
    const { ctx } = this;
    const { pagenumber, limit } = ctx.query;
    if (!pagenumber) {
      ctx.body = { code: 501, msg: '缺少必填参数,请输入pagenumber字段' };
    } else if (!limit) {
      ctx.body = { code: 501, msg: '缺少必填参数,请输入limit字段' };
    } else {
      const data = await ctx.service.userService.limitUser(pagenumber, limit);
      ctx.body = { code: 200, data };
    }
  }
}
module.exports = UserController;
